package com.fick.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.MageeditionDAO;
import com.fick.hibernate.dao.MagephaseDAO;
import com.fick.hibernate.dao.MagezineDAO;
import com.fick.hibernate.model.Mageedition;
import com.fick.hibernate.model.Magephase;
import com.fick.hibernate.model.Magezine;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

import java.io.IOException;
import java.util.List;
public class ManageMagezineAction extends DispatchAction{
	
	MagezineDAO magezineDAO = new MagezineDAO();
	MagephaseDAO phaseDAO = new MagephaseDAO();
	MageeditionDAO editionDAO = new MageeditionDAO();
	
	public ActionForward getList(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			
			
			List magezineList = magezineDAO.findAll();
			List phaseList = phaseDAO.findAll();
			List editionList = editionDAO.findAll();
			
			request.setAttribute("magezineList", magezineList);
			request.setAttribute("phaseList", phaseList);
			request.setAttribute("editionList", editionList);
			
			return mapping.findForward("mageList");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
	}
	public ActionForward manage(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws IOException{
		
		String lei = General.convertNullToEmpty(request.getParameter("lei"));

		String buttons = General.convertNullToEmpty(request.getParameter("but"));

		if (buttons.equals("add")) {
			// 添加大类
			if (lei.equals("0")) {
				addPhase(mapping, form, request, response);
				request.setAttribute("message","添加章节成功");
				return getList(mapping, form, request, response);
			}
			// 添加小类
			if (lei.equals("1")) {
				addEdition(mapping, form, request, response);
				request.setAttribute("message","添加版面成功");
				return getList(mapping, form, request, response);
			}
			if (lei.equals("")||lei.equals("-1")) {
				addMagezine(mapping, form, request, response);
				request.setAttribute("message","添加期刊成功");
				return getList(mapping, form, request, response);
			}
		} else {
			// 当是修改按钮时
			if (buttons.equals("update")) {
				if (lei.equals("0")) {
					upMagezine(mapping, form, request, response);
					request.setAttribute("message","修改期刊成功");
					return getList(mapping, form, request, response);
				}
				if (lei.equals("1")) {
					upPhase(mapping, form, request, response);
					request.setAttribute("message","修改章节成功");
					return getList(mapping, form, request, response);
				}
				if (lei.equals("2")) {
					upEdition(mapping, form, request, response);
					request.setAttribute("message","修改版面成功");
					return getList(mapping, form, request, response);
				}
			} else {
				// 当是删除按钮时
				if (lei.equals("0")) {
					delMagezine(mapping, form, request, response);
					request.setAttribute("message","删除期刊成功");
					return getList(mapping, form, request, response);
				}
				if (lei.equals("1")) {
					delPhase(mapping, form, request, response);
					request.setAttribute("message","删除章节成功");
					return getList(mapping, form, request, response);
				}
				if (lei.equals("2")) {
					delEdition(mapping, form, request, response);
					request.setAttribute("message","删除版面成功");
					return getList(mapping, form, request, response);
				}
			}
		}
		return null;
	}
	//增加
	public ActionForward addMagezine(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
		MagezineDAO magezineDAO = new MagezineDAO();
		Magezine magezine = new Magezine();
		magezine.setName(leiName);
		magezine.setMageorder(Integer.valueOf(order));
		magezineDAO.save(magezine);
		
		
		return null;
	}
	public ActionForward addPhase(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String magezineid = request.getParameter("mageid");
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
		
		Magezine magezine = magezineDAO.findById(Integer.valueOf(magezineid));
		
		
		Magephase phase = new Magephase();
		phase.setName(leiName);
		phase.setMagezine(magezine);
		phase.setPhaseorder(Integer.valueOf(order));
		phaseDAO.save(phase);
		
		
		
		return null;
	}
	public ActionForward addEdition(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		
		String phaseid = request.getParameter("phaseid");
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
		
		Magephase phase = phaseDAO.findById(Integer.valueOf(phaseid));
		
		Mageedition edition = new Mageedition();
		edition.setName(leiName);
		edition.setEditionorder(Integer.valueOf(order));
		edition.setMagephase(phase);
		editionDAO.save(edition);
		
		
		
		return null;
	}
	
	//修改
	public ActionForward upMagezine(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String magezineid = request.getParameter("mageid");
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
	
		Magezine magezine = magezineDAO.findById(Integer.valueOf(magezineid));
		magezine.setMageorder(Integer.valueOf(order));
		magezine.setName(leiName);
		
		magezineDAO.update(magezine);
		
		
		return null;
	}
	public ActionForward upPhase(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String phaseid = request.getParameter("phaseid");
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
		
		Magephase phase = phaseDAO.findById(Integer.valueOf(phaseid));
		phase.setName(leiName);
		phase.setPhaseorder(Integer.valueOf(order));
		
		magezineDAO.update(phase);
		
		return null;
	}
	public ActionForward upEdition(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String editionid = request.getParameter("editionid");
		String leiName = request.getParameter("leiName");
		String order = request.getParameter("order");
		
		Mageedition edition = editionDAO.findById(Integer.valueOf(editionid));
		edition.setName(leiName);
		edition.setEditionorder(Integer.valueOf(order));

		editionDAO.update(edition);
		
		return null;
	}
	
	
	//删除
	public ActionForward delMagezine(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String magezineid = request.getParameter("mageid");
		Magezine magezine = magezineDAO.findById(Integer.valueOf(magezineid));
		magezineDAO.delete(magezine);
		
		
		return null;
	}
	public ActionForward delPhase(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String phaseid = request.getParameter("phaseid");
		Magephase phase = phaseDAO.findById(Integer.valueOf(phaseid));
		phaseDAO.delete(phase);
		
		
		return null;
	}
	public ActionForward delEdition(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String editionid = request.getParameter("editionid");
		Mageedition edition = editionDAO.findById(Integer.valueOf(editionid));
		editionDAO.delete(edition);
		
		return null;
	}
}
